package com.ng.desktop.view.categoria;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;

import com.ng.desktop.model.Categoria;
import com.ng.desktop.persistence.CategoriaPersistence;
import com.ng.framework.view.desktop.NGEditDialog;

public class CategoriaEditDialog extends NGEditDialog<Categoria, CategoriaPersistence> {
	
	private static final long serialVersionUID = -8491323457814105705L;
	
	private static final NumberFormat DECIMAL_FORMAT = new DecimalFormat("0.00");
	
	private JTextField textFieldId;
	private JTextField textFieldNombre;
	private JTextField textFieldEnergia;
	private JTextField textFieldChon;
	private JTextField textFieldCho;
	private JTextField textFieldFat;

	public CategoriaEditDialog(Frame owner, CategoriaPersistence persistence) {
		this(owner, persistence, null);
	}

	public CategoriaEditDialog(Frame owner, CategoriaPersistence persistence, Categoria instance) {
		super(owner, persistence, instance);
		
		this.setTitle("Gestión de Categoría");
		this.setSize(new Dimension(300, 250));
	}
	
	@Override
	protected JPanel createDetailPanel() {
		this.textFieldId = new JTextField(5);
		textFieldId.setEditable(false);
		textFieldId.setFocusable(false);
		textFieldId.setHorizontalAlignment(JTextField.RIGHT);
		this.textFieldNombre = new JTextField();
		this.textFieldEnergia = new JTextField(5);
		textFieldEnergia.setHorizontalAlignment(JTextField.RIGHT);
		this.textFieldChon = new JTextField(5);
		textFieldChon.setHorizontalAlignment(JTextField.RIGHT);
		this.textFieldCho = new JTextField(5);
		textFieldCho.setHorizontalAlignment(JTextField.RIGHT);
		this.textFieldFat = new JTextField(5);
		textFieldFat.setHorizontalAlignment(JTextField.RIGHT);
		
		JPanel detailPanel = new JPanel(new GridBagLayout());
		
		detailPanel.add(new JLabel("Id:"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldId, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));
		detailPanel.add(new JLabel("Nombre:"), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldNombre, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
		detailPanel.add(new JLabel("Energía:"), new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldEnergia, new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));
		detailPanel.add(new JLabel("CHON:"), new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldChon, new GridBagConstraints(1, 3, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));
		detailPanel.add(new JLabel("CHO:"), new GridBagConstraints(0, 4, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldCho, new GridBagConstraints(1, 4, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));
		detailPanel.add(new JLabel("FAT:"), new GridBagConstraints(0, 5, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 5), 0, 0));
		detailPanel.add(textFieldFat, new GridBagConstraints(1, 5, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		
		return detailPanel;
	}

	@Override
	protected boolean validateInput() {
		boolean valid = true;
		
		if(StringUtils.isEmpty(textFieldNombre.getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'Nombre' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldNombre.requestFocus();
			
			valid = false;
		} else if(StringUtils.isEmpty(textFieldEnergia.getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'Energía' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldEnergia.requestFocus();
			
			valid = false;
		} else if(!validateDouble(textFieldEnergia.getText())) {
			JOptionPane.showMessageDialog(this, "El contenido del campo 'Energía' es inválido.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldEnergia.requestFocus();
			
			valid = false;
		} else if(StringUtils.isEmpty(textFieldChon.getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'CHON' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldChon.requestFocus();
			
			valid = false;
		} else if(!validateDouble(textFieldChon.getText())) {
			JOptionPane.showMessageDialog(this, "El contenido del campo 'CHON' es inválido.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldChon.requestFocus();
			
			valid = false;
		} else if(StringUtils.isEmpty(textFieldCho.getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'CHO' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldCho.requestFocus();
			
			valid = false;
		} else if(!validateDouble(textFieldCho.getText())) {
			JOptionPane.showMessageDialog(this, "El contenido del campo 'CHO' es inválido.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldCho.requestFocus();
			
			valid = false;
		} else if(StringUtils.isEmpty(textFieldFat.getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'FAT' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldFat.requestFocus();
			
			valid = false;
		} else if(!validateDouble(textFieldFat.getText())) {
			JOptionPane.showMessageDialog(this, "El contenido del campo 'FAT' es inválido.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldFat.requestFocus();
			
			valid = false;
		}
		return valid;
	}
	
	private boolean validateDouble(final String s) {
		boolean valid = true;
		
		try {
			Double.parseDouble(s);			
		} catch(NumberFormatException e) {
			valid = false;
		}
		
		return valid;
	}	

	@Override
	protected Categoria createNewInstance() {
		return new Categoria();
	}

	@Override
	protected void populateInstance(Categoria instance) {
		instance.setCho(Double.parseDouble(textFieldCho.getText()));
		instance.setChon(Double.parseDouble(textFieldChon.getText()));
		instance.setEnergia(Double.parseDouble(textFieldEnergia.getText()));
		instance.setFat(Double.parseDouble(textFieldFat.getText()));
		instance.setNombre(textFieldNombre.getText());
	}

	@Override
	protected void populateDetailPanel(Categoria instance) {
		textFieldCho.setText(DECIMAL_FORMAT.format(instance.getCho()));
		textFieldChon.setText(DECIMAL_FORMAT.format(instance.getChon()));
		textFieldEnergia.setText(DECIMAL_FORMAT.format(instance.getEnergia()));
		textFieldFat.setText(DECIMAL_FORMAT.format(instance.getFat()));
		textFieldId.setText(String.valueOf(instance.getId()));
		textFieldNombre.setText(instance.getNombre());
	}

}
